package com.example.demo.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.domain.Contacts;
import com.example.demo.domain.User;
import com.example.demo.service.ContactsService;
import com.example.demo.mapper.ContactsMapper;
import com.example.demo.service.UserService;
import com.example.demo.utils.Result;
import com.example.demo.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 86187
* @description 针对表【contacts】的数据库操作Service实现
* @createDate 2025-06-18 16:47:33
*/
@Service
@RequiredArgsConstructor
public class ContactsServiceImpl extends ServiceImpl<ContactsMapper, Contacts> implements ContactsService{

    private final UserService userService;

    @Override
    public void addContact(Contacts contacts) {
        // 调用 Mapper 层插入数据
        baseMapper.insert(contacts);
    }

    @Override
    public Result<List<Contacts>> queryContactByUserId(HttpServletRequest request) {
        // 从token中获取当前登录用户信息
        String token = request.getHeader("authorization");
        Map<String, String> tokenMap = TokenUtil.parseToken(token);
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getUserName, tokenMap.get("userName"));
        User currentUser = userService.getOne(userWrapper);
        
        if (currentUser == null) {
            return Result.error("用户未登录或不存在");
        }

        // 查询当前用户的所有联系人
        LambdaQueryWrapper<Contacts> contactsWrapper = new LambdaQueryWrapper<>();
        contactsWrapper.eq(Contacts::getUserId, currentUser.getId());
        List<Contacts> contactsList = list(contactsWrapper);
        
        if (CollUtil.isEmpty(contactsList)) {
            return Result.success(Collections.emptyList());
        }
        
        // 获取所有联系人的ID
        List<Long> contactUserIds = contactsList.stream()
            .map(Contacts::getContactId)
            .collect(Collectors.toList());
        
        // 查询所有联系人的用户信息
        LambdaQueryWrapper<User> contactUserWrapper = new LambdaQueryWrapper<>();
        contactUserWrapper.in(User::getId, contactUserIds);
        List<User> contactUsers = userService.list(contactUserWrapper);
        
        // 将用户信息关联到联系人对象中
        Map<Integer, User> userMap = contactUsers.stream()
            .collect(Collectors.toMap(User::getId, user -> user));
        
        // 为每个联系人设置用户详细信息
        for (Contacts contact : contactsList) {
            User contactUser = userMap.get(contact.getContactId().intValue());
            if (contactUser != null) {
                contact.setContactUser(contactUser);
            }
        }
        
        return Result.success(contactsList);
    }
}




